import 'package:flutter/material.dart';
import '../refresh_page.dart';
import 'typedefs.dart';

class RefreshPage<C extends RefreshPageController> extends StatelessWidget {
  final bool enablePullUp;
  final bool enablePullDown;
  final String? tag;
  final Widget Function(C) childBuilder;

  /// 自定义加载中状态构建器，优先级高于全局配置
  final LoadingBuilder? loadingBuilder;

  /// 自定义空状态构建器，优先级高于全局配置
  final EmptyBuilder? emptyBuilder;

  /// 自定义错误状态构建器，优先级高于全局配置
  final ErrorBuilder? errorBuilder;

  /// 自定义状态显示位置构建器，允许用户自定义状态显示位置
  final Widget Function(Widget stateWidget)? statePositionBuilder;

  /// 空状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool emptyOverlay;

  /// 错误状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool errorOverlay;

  /// 加载状态是否覆盖整个区域，为false时通过statePositionBuilder回调
  final bool loadingOverlay;

  const RefreshPage({
    super.key,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.tag,
    required this.childBuilder,
    this.loadingBuilder,
    this.emptyBuilder,
    this.errorBuilder,
    this.statePositionBuilder,
    this.emptyOverlay = true,
    this.errorOverlay = true,
    this.loadingOverlay = true,
  });

  @override
  Widget build(BuildContext context) {
    final pageController = Get.find<C>(tag: tag);
    return GetBuilder<C>(
      tag: tag,
      id: pageController.refreshId,
      builder: (_) {
        return RefreshWidget<C>(
          tag: tag,
          enablePullUp: enablePullUp,
          enablePullDown: enablePullDown,
          loadingBuilder: loadingBuilder,
          emptyBuilder: emptyBuilder,
          errorBuilder: errorBuilder,
          statePositionBuilder: statePositionBuilder,
          emptyOverlay: emptyOverlay,
          errorOverlay: errorOverlay,
          loadingOverlay: loadingOverlay,
          child: childBuilder(pageController),
        );
      },
    );
  }
}
